home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / fileutil.13 / fileutil / fileutils-3.13 / src / chmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-16  |  8.3 KB  |  333 lines

  1. /* chmod -- change permission modes of files
  2.    Copyright (C) 89, 90, 91, 95, 1996 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software Foundation,
  16.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. /* Options:
  19.    -R    Recursively change modes of directory contents.
  20.    -c    Verbosely describe only files whose modes actually change.
  21.    -f    Do not print error messages about files.
  22.    -v    Verbosely describe changed modes.
  23.  
  24.    David MacKenzie <djm@gnu.ai.mit.edu> */
  25.  
  26. #include <config.h>
  27. #include <stdio.h>
  28. #include <getopt.h>
  29. #include <sys/types.h>
  30.  
  31. #include "modechange.h"
  32. #include "system.h"
  33. #include "error.h"
  34.  
  35. void mode_string ();
  36. char *savedir ();
  37. void strip_trailing_slashes ();
  38. char *xmalloc ();
  39. char *xrealloc ();
  40.  
  41. static int change_dir_mode __P ((const char *dir,
  42.                  const struct mode_change *changes,
  43.                  const struct stat *statp));
  44.  
  45. /* The name the program was run with. */
  46. char *program_name;
  47.  
  48. /* If nonzero, change the modes of directories recursively. */
  49. static int recurse;
  50.  
  51. /* If nonzero, force silence (no error messages). */
  52. static int force_silent;
  53.  
  54. /* If nonzero, describe the modes we set. */
  55. static int verbose;
  56.  
  57. /* If nonzero, describe only modes that change. */
  58. static int changes_only;
  59.  
  60. /* If nonzero, display usage information and exit.  */
  61. static int show_help;
  62.  
  63. /* If nonzero, print the version on standard output and exit.  */
  64. static int show_version;
  65.  
  66. static struct option const long_options[] =
  67. {
  68.   {"recursive", no_argument, 0, 'R'},
  69.   {"changes", no_argument, 0, 'c'},
  70.   {"silent", no_argument, 0, 'f'},
  71.   {"quiet", no_argument, 0, 'f'},
  72.   {"verbose", no_argument, 0, 'v'},
  73.   {"help", no_argument, &show_help, 1},
  74.   {"version", no_argument, &show_version, 1},
  75.   {0, 0, 0, 0}
  76. };
  77.  
  78. /* Tell the user the mode MODE that file FILE has been set to;
  79.    if CHANGED is zero, FILE had that mode already. */
  80.  
  81. static void
  82. describe_change (const char *file, short unsigned int mode, int changed)
  83. {
  84.   char perms[11];        /* "-rwxrwxrwx" ls-style modes. */
  85.  
  86.   mode_string (mode, perms);
  87.   perms[10] = '\0';        /* `mode_string' does not null terminate. */
  88.   if (changed)
  89.     printf (_("mode of %s changed to %04o (%s)\n"),
  90.         file, mode & 07777, &perms[1]);
  91.   else
  92.     printf (_("mode of %s retained as %04o (%s)\n"),
  93.         file, mode & 07777, &perms[1]);
  94. }
  95.  
  96. /* Change the mode of FILE according to the list of operations CHANGES.
  97.    If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
  98.    mode of the referenced file.  If DEREF_SYMLINK is zero, ignore symbolic
  99.    links.  Return 0 if successful, 1 if errors occurred. */
  100.  
  101. static int
  102. change_file_mode (const char *file, const struct mode_change *changes,
  103.           const int deref_symlink)
  104. {
  105.   struct stat file_stats;
  106.   unsigned short newmode;
  107.   int errors = 0;
  108.  
  109.   if (lstat (file, &file_stats))
  110.     {
  111.       if (force_silent == 0)
  112.     error (0, errno, "%s", file);
  113.       return 1;
  114.     }
  115. #ifdef S_ISLNK
  116.   if (S_ISLNK (file_stats.st_mode))
  117.     {
  118.       if (! deref_symlink)
  119.     return 0;
  120.       else
  121.     if (stat (file, &file_stats))
  122.       {
  123.         if (force_silent == 0)
  124.           error (0, errno, "%s", file);
  125.         return 1;
  126.       }
  127.     }
  128. #endif
  129.  
  130.   newmode = mode_adjust (file_stats.st_mode, changes);
  131.  
  132.   if (newmode != (file_stats.st_mode & 07777))
  133.     {
  134.       if (verbose)
  135.     describe_change (file, newmode, 1);
  136.       if (chmod (file, (int) newmode))
  137.     {
  138.       if (force_silent == 0)
  139.         error (0, errno, "%s", file);
  140.       errors = 1;
  141.     }
  142.     }
  143.   else if (verbose && changes_only == 0)
  144.     describe_change (file, newmode, 0);
  145.  
  146.   if (recurse && S_ISDIR (file_stats.st_mode))
  147.     errors |= change_dir_mode (file, changes, &file_stats);
  148.   return errors;
  149. }
  150.  
  151. /* Recursively change the modes of the files in directory DIR
  152.    according to the list of operations CHANGES.
  153.    STATP points to the results of lstat on DIR.
  154.    Return 0 if successful, 1 if errors occurred. */
  155.  
  156. static int
  157. change_dir_mode (const char *dir, const struct mode_change *changes,
  158.          const struct stat *statp)
  159. {
  160.   char *name_space, *namep;
  161.   char *path;            /* Full path of each entry to process. */
  162.   unsigned dirlength;        /* Length of DIR and '\0'. */
  163.   unsigned filelength;        /* Length of each pathname to process. */
  164.   unsigned pathlength;        /* Bytes allocated for `path'. */
  165.   int errors = 0;
  166.  
  167.   errno = 0;
  168.   name_space = savedir (dir, statp->st_size);
  169.   if (name_space == NULL)
  170.     {
  171.       if (errno)
  172.     {
  173.       if (force_silent == 0)
  174.         error (0, errno, "%s", dir);
  175.       return 1;
  176.     }
  177.       else
  178.     error (1, 0, _("virtual memory exhausted"));
  179.     }
  180.  
  181.   dirlength = strlen (dir) + 1;    /* + 1 is for the trailing '/'. */
  182.   pathlength = dirlength + 1;
  183.   /* Give `path' a dummy value; it will be reallocated before first use. */
  184.   path = xmalloc (pathlength);
  185.   strcpy (path, dir);
  186.   path[dirlength - 1] = '/';
  187.  
  188.   for (namep = name_space; *namep; namep += filelength - dirlength)
  189.     {
  190.       filelength = dirlength + strlen (namep) + 1;
  191.       if (filelength > pathlength)
  192.     {
  193.       pathlength = filelength * 2;
  194.       path = xrealloc (path, pathlength);
  195.     }
  196.       strcpy (path + dirlength, namep);
  197.       errors |= change_file_mode (path, changes, 0);
  198.     }
  199.   free (path);
  200.   free (name_space);
  201.   return errors;
  202. }
  203.  
  204. static void
  205. usage (int status)
  206. {
  207.   if (status != 0)
  208.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  209.          program_name);
  210.   else
  211.     {
  212.       printf (_("\
  213. Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
  214.   or:  %s [OPTION]... OCTAL_MODE FILE...\n\
  215. "),
  216.           program_name, program_name);
  217.       printf (_("\
  218. \n\
  219.   -c, --changes           like verbose but report only when a change is made\n\
  220.   -f, --silent, --quiet   suppress most error messages\n\
  221.   -v, --verbose           output a diagnostic for every file processed\n\
  222.   -R, --recursive         change files and directories recursively\n\
  223.       --help              display this help and exit\n\
  224.       --version           output version information and exit\n\
  225. \n\
  226. Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
  227. one or more of the letters rwxXstugo.\n"));
  228.     }
  229.   exit (status);
  230. }
  231.  
  232. /* Parse the ASCII mode given on the command line into a linked list
  233.    of `struct mode_change' and apply that to each file argument. */
  234.  
  235. int
  236. main (int argc, char **argv)
  237. {
  238.   struct mode_change *changes;
  239.   int errors = 0;
  240.   int modeind = 0;        /* Index of the mode argument in `argv'. */
  241.   int thisind;
  242.   int c;
  243.  
  244.   program_name = argv[0];
  245.   setlocale (LC_ALL, "");
  246.   bindtextdomain (PACKAGE, LOCALEDIR);
  247.   textdomain (PACKAGE);
  248.  
  249.   recurse = force_silent = verbose = changes_only = 0;
  250.  
  251.   while (1)
  252.     {
  253.       thisind = optind ? optind : 1;
  254.  
  255.       c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
  256.                (int *) 0);
  257.       if (c == EOF)
  258.     break;
  259.  
  260.       switch (c)
  261.     {
  262.     case 0:
  263.       break;
  264.     case 'r':
  265.     case 'w':
  266.     case 'x':
  267.     case 'X':
  268.     case 's':
  269.     case 't':
  270.     case 'u':
  271.     case 'g':
  272.     case 'o':
  273.     case 'a':
  274.     case ',':
  275.     case '+':
  276.     case '-':
  277.     case '=':
  278.       if (modeind != 0 && modeind != thisind)
  279.         error (1, 0, _("invalid mode"));
  280.       modeind = thisind;
  281.       break;
  282.     case 'R':
  283.       recurse = 1;
  284.       break;
  285.     case 'c':
  286.       verbose = 1;
  287.       changes_only = 1;
  288.       break;
  289.     case 'f':
  290.       force_silent = 1;
  291.       break;
  292.     case 'v':
  293.       verbose = 1;
  294.       break;
  295.     default:
  296.       usage (1);
  297.     }
  298.     }
  299.  
  300.   if (show_version)
  301.     {
  302.       printf ("chmod - %s\n", PACKAGE_VERSION);
  303.       exit (0);
  304.     }
  305.  
  306.   if (show_help)
  307.     usage (0);
  308.  
  309.   if (modeind == 0)
  310.     modeind = optind++;
  311.  
  312.   if (optind >= argc)
  313.     {
  314.       error (0, 0, _("too few arguments"));
  315.       usage (1);
  316.     }
  317.  
  318.   changes = mode_compile (argv[modeind],
  319.               MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
  320.   if (changes == MODE_INVALID)
  321.     error (1, 0, _("invalid mode"));
  322.   else if (changes == MODE_MEMORY_EXHAUSTED)
  323.     error (1, 0, _("virtual memory exhausted"));
  324.  
  325.   for (; optind < argc; ++optind)
  326.     {
  327.       strip_trailing_slashes (argv[optind]);
  328.       errors |= change_file_mode (argv[optind], changes, 1);
  329.     }
  330.  
  331.   exit (errors);
  332. }
  333.